package com.mazaiting.blog.controller

import com.mazaiting.blog.domain.Visitor
import com.mazaiting.blog.service.VisitorService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import java.util.*
import javax.servlet.http.HttpServletRequest

@Controller
class HomeController {

    /**日志框架*/
    val logger = LoggerFactory.getLogger(javaClass)!!

    /**
     * 访问者服务
     */
    @Autowired
    lateinit var service: VisitorService

    /**
     * 访问首页方式
     * 切记不能配置index.html
     * @param request 网络请求
     * @return 页面
     */
    @GetMapping("/", "/index")
    fun index(request: HttpServletRequest) : String{
        // 获取相关信息，创建访问者
        val visitor = Visitor(request.remoteAddr, request.remoteHost, request.remotePort)
        // 保存访问者，并返回是否允许访问
        val isAllow = service.saveVisitor(visitor)
        return if (isAllow) "index"
            else ""
    }
}